(function() {
  app.controller('detail_zsCtrl', function($scope, quoteApi, $interval, canvas, kchart, $rootScope, header_m, my_zxg_v) {
    var detail_codeinfo = $scope.storage.local('detail_codeinfo');
    header_m.set({
      title_c: detail_codeinfo.code,
      title2_c: detail_codeinfo.name,
    });

    var k_chart = {
      k_type: 0x21,
      sublist: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
    };
    angular.extend(k_chart, detail_codeinfo);

    var fenshi = angular.copy(k_chart);
    $scope.market_tab_actived = '1';
    $scope.wudang_actived = '1';
    $scope.show_min = '分钟';
    canvas.canvas(fenshi);
    $scope.$on('AddressUpdated', function() {
      // 监听地址变化并获取相应数据
      angular.extend($scope.order_detail, $rootScope.detail_order);
    });

    var p = new quoteApi.QuoteApi.Price0x21({ //  数据
      code: k_chart.code,
      market: k_chart.market,
      sublist: [4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 38, 39, 40, 46]
    });
    var d = new quoteApi.QuoteApi.Detail0x26({ // tab里明细
      code: k_chart.code,
      market: k_chart.market,
      length: 16,
    });

    $scope.init = function() {

      quoteApi.query(p).then(function(data) {
        $scope.order_detail = data;
      });

      quoteApi.query(d).then(function(data) {
        $scope.detail = data;
      });

      var obj = {
        '1A0001': 1101, ///沪深A股
        '1A0002': 1102, ///上海A股
        //			'1A0001':1103, ///深圳A股
        //			'1A0001':1104, ///主板A股
        // '399005': 1105, ///中小企业板
        '399006': 1106, ///创业板
        //			'1A0001':1107, ///全部B股
        //			'1A0001':1108, ///上海B股
        //			'1A0001':1109, ///深圳B股
        //			'1A0001':1110, ///全部AB股
        //			'1A0001':1111, ///三板股
        '1B0016': 1112, ///上证50
        '1B0007': 1113, ///上证180
        '399001': 1114, ///深证成指
        //			'399001':1115, ///深证100
        '1B0300': 1116, ///沪深300
        '399106': 1114, ///深证综指,
      };


      var q = [{
          sort_index: 34
        },
        {
          sort_index: 34,
          sort_mode: 1
        },
        {
          sort_index: 37
        },
      ];
      for (var i = 0; i < q.length; i++) { // 指数行情里的榜单
        var that = i;
        q[i].board_id = obj[k_chart.code] || 1102;
        q[i].sublist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 33, 34, 37];
        var req = new quoteApi.QuoteApi.BoardRank0x28(q[i]);
        (function(req, i) {
          quoteApi.query(req).then(function(data) {
            if (i === 0) $scope.zhangfubang = data.data;
            if (i === 1) $scope.diefubang = data.data;
            if (i === 2) $scope.hslbang = data.data;

          });
        })(req, i);

      }

    };

    var timer = $interval(function() {
      $scope.init();
    }, 3000);

    $scope.$on('$destroy', function() {
      $interval.cancel(timer);
      timer = null;
    });

    $scope.market_tab = function(index) {
      $scope.market_tab_actived = index;
      $scope.select_m = false;
      $('#buttons_opear').hide();
      if (index === '4') {
        k_chart.k_type = 0x27;
      } else if (index === '5') {
        k_chart.k_type = 0x28;
      } else if (index === '3') {
        k_chart.k_type = 0x21;
      } else if (index === '1') {
        canvas.canvas(fenshi);
        return;
      } else {
        return;
      }
      kchart.canvas2(k_chart, true);
    };

    $scope.select_min = function(param) {
      $scope.market_tab_actived = '6';
      $scope.select_m = false;
      $scope.show_min = param + '分钟';
      if (param === '1') {
        k_chart.k_type = 0X22;
      } else if (param === '5') {
        k_chart.k_type = 0X23;
      } else if (param === '15') {
        k_chart.k_type = 0X24;
      } else if (param === '30') {
        k_chart.k_type = 0X25;
      } else if (param === '60') {
        k_chart.k_type = 0X26;
      } else {
        return;
      }
      kchart.canvas2(k_chart, true);
    };

    var my_zxg;
    my_zxg_v
      .updata()
      .then(function(res) {
        my_zxg = res;
        if (my_zxg_v.check(detail_codeinfo) !== -1) {
          $scope.my_zxg_exist = true;
        }
      });

    $scope.add_zx = function() {
      my_zxg_v
        .set(detail_codeinfo, { toggle: true })
        .then(function(res) {
          $scope.my_zxg_exist = !$scope.my_zxg_exist;
        });
    };

  });
})(app);